package com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.OwnScrollView;
import com.diudiutechnology.www.yadiudiu.util.view.item.VehicleDetailsLayoutView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/19.
 * 汽车详情
 */

public class WashCarDetailsActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)//返回
            LinearLayout retur;
    @Bind(R.id.back)//返回
            LinearLayout back;

    Context context;
    @Bind(R.id.scroll_view)
    OwnScrollView scrollView;
    @Bind(R.id.title)
    RelativeLayout title;


    @Bind(R.id.contentLayout)//车辆详情 和 评价
    LinearLayout contentLayout;

    @Bind(R.id.vehicle_details)//车辆详情
            TextView vehicleDetails;
    @Bind(R.id.vehicle_details_xiala)
    ImageView vehicleDetailsXiala;

    @Bind(R.id.evaluation_text)//评价
            TextView evaluationText;
    @Bind(R.id.evaluation_img)
    ImageView evaluationImg;


    @Bind(R.id.car_loans_calculate_layout)//车贷计算器
            RelativeLayout carLoansCalculateLayout;


    @Bind(R.id.ad_pager)//轮播
            ViewPager adPager;
    @Bind(R.id.ad_layer)
    LinearLayout adLayer;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    VehicleDetailsLayoutView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wash_car_details);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        back.setOnClickListener(this);
        vehicleDetails.setOnClickListener(this);
        evaluationText.setOnClickListener(this);
        carLoansCalculateLayout.setOnClickListener(this);
        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);

        scrollView.setScrollViewListener(new OwnScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(OwnScrollView scrollView, int x, int y, int oldx, int oldy) {
                int diffHeight = title.getHeight() * 2;// 位移距离2倍titleBar高度,其他app貌似是1X
                if (Math.abs(y) < diffHeight) {// 位置距离小于diff,设置alpha差值
                    float alpha = (diffHeight - Math.abs(y)) / (diffHeight * 1f);
                    title.setAlpha(Math.abs(1 - alpha));
                }
                if (Math.abs(y) > diffHeight) {// 位移距离大于diff，直接设置为1
                    title.setAlpha(1);
                }
            }
        });
         view = new VehicleDetailsLayoutView(context);
        contentLayout.addView(view);
        processVehicleDetails();

    }
    //添加车辆详情
    private void processVehicleDetails(){
        view.setData(0);
    }
    //添加评论
    private void processEvaluation(){
        view.setData(1);
    }

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur://返回
                finish();
                break;
            case R.id.back://返回
                finish();
                break;
            case R.id.vehicle_details: //车辆详情
                vehicleDetailsXiala.setVisibility(View.VISIBLE);
                evaluationImg.setVisibility(View.GONE);
                processVehicleDetails();
                break;
            case R.id.evaluation_text://全部评价
                vehicleDetailsXiala.setVisibility(View.GONE);
                evaluationImg.setVisibility(View.VISIBLE);
                processEvaluation();
                break;
            case R.id.car_loans_calculate_layout://车贷计算器
            startActivity(new Intent(context,CarLoanCalculatorActivity.class));

                break;

        }
    }
}
